﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace School.UserCentents
{
    public partial class Member : UserCheckPower
    {
        #region 分页相关

        //获取传入当前页码
        int PageIndex
        {
            get
            {
                if (Request.QueryString.Get("page") != null)
                {
                    if (!Common.TypeValidator.IsNumeric(Request.QueryString.Get("page")))
                    {
                        return 1;
                    }
                    return Convert.ToInt32(Request.QueryString.Get("page"));
                }
                return 1;
            }
        }

        //分页数
        private int PageSize
        {
            get
            {
                return 10;
            }
        }
        #endregion
        BLL.Users Users = new BLL.Users();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (new BLL.Users().GetModel(LoginUserId).UserTypeID <= 3)
            {
                Panel p1 = (Panel)Master.FindControl("Panel1");
                Panel p2 = (Panel)Master.FindControl("Panel2");
                Panel p3 = (Panel)Master.FindControl("Panel3");
                Panel p4 = (Panel)Master.FindControl("Panel4");
                p1.CssClass = "sldmenuoff";
                p2.CssClass = "sldmenuon0";
                p3.CssClass = "sld1";
                p4.CssClass = "sld0";
                p3.Attributes.Add("style", "display: none");
                p4.Attributes.Add("style", "");
            }
            if (!IsPostBack)
            {
                PageLoad();
            }
        }

        private void PageLoad()
        {
            string strWhere = " and Remark=" + LoginUserId;
            DataSet ds = Users.GetList("Remark=" + LoginUserId);
            Model.Users u = Users.GetModel(LoginUserId);
            if (u.UserTypeID == 1)
            {
                LinkButton2.Visible = LinkButton6.Visible = true;
                strWhere = "";
                ds = Users.GetAllList();

                LinkButton6.Text = "代区(" + Users.GetList("UserTypeID=2").Tables[0].Rows.Count + ")";
                LinkButton2.Text = "校长(" + Users.GetList("UserTypeID=3").Tables[0].Rows.Count + ")";
                LinkButton3.Text = "老师(" + Users.GetList("UserTypeID=4").Tables[0].Rows.Count + ")";
                LinkButton4.Text = "学生(" + Users.GetList("UserTypeID=6").Tables[0].Rows.Count + ")";
                LinkButton5.Text = "家长(" + Users.GetList("UserTypeID=5").Tables[0].Rows.Count + ")";
            }
            else if (u.UserTypeID == 2)
            {
                LinkButton2.Visible = true;

                LinkButton2.Text = "校长(" + Users.GetList("UserTypeID=3 and Remark=" + LoginUserId).Tables[0].Rows.Count + ")";
                LinkButton3.Text = "老师(" + Users.GetList("UserTypeID=4 and Remark=" + LoginUserId).Tables[0].Rows.Count + ")";
                LinkButton4.Text = "学生(" + Users.GetList("UserTypeID=6 and Remark=" + LoginUserId).Tables[0].Rows.Count + ")";
                LinkButton5.Text = "家长(" + Users.GetList("UserTypeID=5 and Remark=" + LoginUserId).Tables[0].Rows.Count + ")";
            }
            else if (u.UserTypeID == 3)
            {
                strWhere = " and Remark2=" + LoginUserId;
                ds = Users.GetList("Remark2=" + LoginUserId);
                LinkButton3.Text = "老师(" + Users.GetList("UserTypeID=4 and Remark2=" + LoginUserId).Tables[0].Rows.Count + ")";
                LinkButton4.Text = "学生(" + Users.GetList("UserTypeID=6 and Remark2=" + LoginUserId).Tables[0].Rows.Count + ")";
                LinkButton5.Text = "家长(" + Users.GetList("UserTypeID=5 and Remark2=" + LoginUserId).Tables[0].Rows.Count + ")";
            }
            else if (u.UserTypeID == 4)
            {
                LinkButton3.Visible = false;
                strWhere = " and CreateUserID=" + LoginUserId;
                ds = Users.GetList("CreateUserID=" + LoginUserId);
                LinkButton3.Visible = false;
                LinkButton4.Text = "学生(" + Users.GetList("UserTypeID=6 and CreateUserID=" + LoginUserId).Tables[0].Rows.Count + ")";
                LinkButton5.Text = "家长(" + Users.GetList("UserTypeID=5 and CreateUserID=" + LoginUserId).Tables[0].Rows.Count + ")";
            }
            else if (u.UserTypeID == 5 || u.UserTypeID == 6)
            {
                LinkButton3.Visible = false;
                strWhere = " and CreateUserID=" + u.CreateUserID;
                ds = Users.GetList("CreateUserID=" + u.CreateUserID);
                LinkButton3.Visible = false;
                LinkButton4.Text = "学生(" + Users.GetList("UserTypeID=6 and CreateUserID=" + u.CreateUserID).Tables[0].Rows.Count + ")";
                LinkButton5.Text = "家长(" + Users.GetList("UserTypeID=5 and CreateUserID=" + u.CreateUserID).Tables[0].Rows.Count + ")";
            }

            if (ds.Tables[0].Rows.Count > 0)
            {
                Repeater1.DataSource = Users.GetList(PageSize, PageIndex, strWhere);
                Repeater1.DataBind();
                AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
                AspNetPager1.PageSize = 10;
                AspNetPager1.CurrentPageIndex = PageIndex;
                AspNetPager1.DataBind();
            }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                Response.Redirect("AddMember.aspx?id=" + e.CommandArgument);
            }
            if (e.CommandName == "Del")
            {
                Users.Delete(int.Parse(e.CommandArgument.ToString()));
                PageLoad();
            }
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lb = (Label)e.Item.FindControl("lbTypeID");
                lb.Text = new BLL.UserType().GetModel(int.Parse(lb.Text)).TypeName;
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            LinkButton1.CssClass = "btn_hover";
            LinkButton6.CssClass = LinkButton2.CssClass = LinkButton3.CssClass = LinkButton4.CssClass = LinkButton5.CssClass = "btn";
            PageLoad();
        }

        protected void LinkButton6_Click(object sender, EventArgs e)
        {
            LinkButton6.CssClass = "btn_hover";
            LinkButton1.CssClass = LinkButton2.CssClass = LinkButton3.CssClass = LinkButton4.CssClass = LinkButton5.CssClass = "btn";
            string strWhere = " and UserTypeID=2 and Remark=" + LoginUserId;
            DataSet ds = Users.GetList("UserTypeID=2 and Remark=" + LoginUserId);
            Repeater1.DataSource = Users.GetList(PageSize, PageIndex, strWhere);
            Repeater1.DataBind();
            AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
            AspNetPager1.PageSize = 10;
            AspNetPager1.CurrentPageIndex = PageIndex;
            AspNetPager1.DataBind();
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            LinkButton2.CssClass = "btn_hover";
            LinkButton1.CssClass = LinkButton6.CssClass = LinkButton3.CssClass = LinkButton4.CssClass = LinkButton5.CssClass = "btn";
            string strWhere = " and UserTypeID=3 and Remark=" + LoginUserId;
            DataSet ds = Users.GetList("UserTypeID=3 and Remark=" + LoginUserId);

            Repeater1.DataSource = Users.GetList(PageSize, PageIndex, strWhere);
            Repeater1.DataBind();
            AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
            AspNetPager1.PageSize = 10;
            AspNetPager1.CurrentPageIndex = PageIndex;
            AspNetPager1.DataBind();
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            LinkButton3.CssClass = "btn_hover";
            LinkButton1.CssClass = LinkButton2.CssClass = LinkButton6.CssClass = LinkButton4.CssClass = LinkButton5.CssClass = "btn";
            
            string strWhere = " and UserTypeID=4 and Remark=" + LoginUserId;
            DataSet ds = Users.GetList("UserTypeID=4 and Remark=" + LoginUserId);

            Model.Users u = Users.GetModel(LoginUserId);
            if (u.UserTypeID == 3)
            {
                strWhere = " and UserTypeID=4 and Remark2=" + LoginUserId;
                ds = Users.GetList("UserTypeID=4 and Remark2=" + LoginUserId);
            }

            Repeater1.DataSource = Users.GetList(PageSize, PageIndex, strWhere);
            Repeater1.DataBind();
            AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
            AspNetPager1.PageSize = 10;
            AspNetPager1.CurrentPageIndex = PageIndex;
            AspNetPager1.DataBind();
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            LinkButton4.CssClass = "btn_hover";
            LinkButton1.CssClass = LinkButton2.CssClass = LinkButton3.CssClass = LinkButton6.CssClass = LinkButton5.CssClass = "btn";
            string strWhere = " and UserTypeID=6 and Remark=" + LoginUserId;
            DataSet ds = Users.GetList("UserTypeID=6 and Remark=" + LoginUserId);

            Model.Users u = Users.GetModel(LoginUserId);
            if (u.UserTypeID == 3)
            {
                strWhere = " and UserTypeID=6 and Remark2=" + LoginUserId;
                ds = Users.GetList("UserTypeID=6 and Remark2=" + LoginUserId);
            }
            else if (u.UserTypeID == 4)
            {
                strWhere = " and UserTypeID=6 and CreateUserID=" + LoginUserId;
                ds = Users.GetList("UserTypeID=6 and CreateUserID=" + LoginUserId);
            }
            else if (u.UserTypeID == 5 || u.UserTypeID == 6)
            {
                strWhere = " and UserTypeID=6 and CreateUserID=" + u.CreateUserID;
                ds = Users.GetList("UserTypeID=6 and CreateUserID=" + u.CreateUserID);
            }

            Repeater1.DataSource = Users.GetList(PageSize, PageIndex, strWhere);
            Repeater1.DataBind();
            AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
            AspNetPager1.PageSize = 10;
            AspNetPager1.CurrentPageIndex = PageIndex;
            AspNetPager1.DataBind();
        }

        protected void LinkButton5_Click(object sender, EventArgs e)
        {
            LinkButton5.CssClass = "btn_hover";
            LinkButton1.CssClass = LinkButton2.CssClass = LinkButton3.CssClass = LinkButton4.CssClass = LinkButton6.CssClass = "btn";
            string strWhere = " and UserTypeID=5 and Remark=" + LoginUserId;
            DataSet ds = Users.GetList("UserTypeID=5 and Remark=" + LoginUserId);

            Model.Users u = Users.GetModel(LoginUserId);
            if (u.UserTypeID == 3)
            {
                strWhere = " and UserTypeID=5 and Remark2=" + LoginUserId;
                ds = Users.GetList("UserTypeID=5 and Remark2=" + LoginUserId);
            }
            else if (u.UserTypeID == 4)
            {
                strWhere = " and UserTypeID=5 and CreateUserID=" + LoginUserId;
                ds = Users.GetList("UserTypeID=5 and CreateUserID=" + LoginUserId);
            }
            else if (u.UserTypeID == 5 || u.UserTypeID == 6)
            {
                strWhere = " and UserTypeID=5 and CreateUserID=" + u.CreateUserID;
                ds = Users.GetList("UserTypeID=5 and CreateUserID=" + u.CreateUserID);
            }

            Repeater1.DataSource = Users.GetList(PageSize, PageIndex, strWhere);
            Repeater1.DataBind();
            AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
            AspNetPager1.PageSize = 10;
            AspNetPager1.CurrentPageIndex = PageIndex;
            AspNetPager1.DataBind();
        }
    }
}